package Threadtest;

class Count{
    private int count = 0;

    void add()
    {
        synchronized(this)
        {
            count++;
        }
    }

    int get()
    {
        return count;
    }

}

public class demo5 {
    public static void main(String[] args) throws InterruptedException {
        Count count = new Count();
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                count.add();
            }
        });

        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                synchronized (count)
                {
                    count.add();
                }
            }
        });
        t1.start();
        t2.start();

        t1.join();
        t2.join();


        System.out.println("count = "+count.get());
    }
}
